{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandas import DataFrame, Series\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "f    6\n",
       "g    7\n",
       "h    8\n",
       "i    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = Series([1,2,3,4,5,6,7,8,9], index=['a','b','c','d','e','f','g','h','i'])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "f    6\n",
       "g    7\n",
       "h    8\n",
       "i    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "i    9\n",
       "h    8\n",
       "g    7\n",
       "f    6\n",
       "e    5\n",
       "d    4\n",
       "c    3\n",
       "b    2\n",
       "a    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.sort_index(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "f    6\n",
       "g    7\n",
       "h    8\n",
       "i    9\n",
       "dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.607429</td>\n",
       "      <td>0.785784</td>\n",
       "      <td>0.431967</td>\n",
       "      <td>0.520277</td>\n",
       "      <td>0.313813</td>\n",
       "      <td>0.934551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.692599</td>\n",
       "      <td>0.152252</td>\n",
       "      <td>0.002561</td>\n",
       "      <td>0.759924</td>\n",
       "      <td>0.669620</td>\n",
       "      <td>0.237597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.359188</td>\n",
       "      <td>0.511416</td>\n",
       "      <td>0.988670</td>\n",
       "      <td>0.888591</td>\n",
       "      <td>0.877439</td>\n",
       "      <td>0.110544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.690845</td>\n",
       "      <td>0.919966</td>\n",
       "      <td>0.811186</td>\n",
       "      <td>0.600167</td>\n",
       "      <td>0.744653</td>\n",
       "      <td>0.052244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.671912</td>\n",
       "      <td>0.702555</td>\n",
       "      <td>0.438069</td>\n",
       "      <td>0.030407</td>\n",
       "      <td>0.300120</td>\n",
       "      <td>0.227341</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4         5\n",
       "0  0.607429  0.785784  0.431967  0.520277  0.313813  0.934551\n",
       "1  0.692599  0.152252  0.002561  0.759924  0.669620  0.237597\n",
       "2  0.359188  0.511416  0.988670  0.888591  0.877439  0.110544\n",
       "3  0.690845  0.919966  0.811186  0.600167  0.744653  0.052244\n",
       "4  0.671912  0.702555  0.438069  0.030407  0.300120  0.227341"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = DataFrame(np.random.rand(30).reshape(5,6))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.671912</td>\n",
       "      <td>0.702555</td>\n",
       "      <td>0.438069</td>\n",
       "      <td>0.030407</td>\n",
       "      <td>0.300120</td>\n",
       "      <td>0.227341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.690845</td>\n",
       "      <td>0.919966</td>\n",
       "      <td>0.811186</td>\n",
       "      <td>0.600167</td>\n",
       "      <td>0.744653</td>\n",
       "      <td>0.052244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.359188</td>\n",
       "      <td>0.511416</td>\n",
       "      <td>0.988670</td>\n",
       "      <td>0.888591</td>\n",
       "      <td>0.877439</td>\n",
       "      <td>0.110544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.692599</td>\n",
       "      <td>0.152252</td>\n",
       "      <td>0.002561</td>\n",
       "      <td>0.759924</td>\n",
       "      <td>0.669620</td>\n",
       "      <td>0.237597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.607429</td>\n",
       "      <td>0.785784</td>\n",
       "      <td>0.431967</td>\n",
       "      <td>0.520277</td>\n",
       "      <td>0.313813</td>\n",
       "      <td>0.934551</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4         5\n",
       "4  0.671912  0.702555  0.438069  0.030407  0.300120  0.227341\n",
       "3  0.690845  0.919966  0.811186  0.600167  0.744653  0.052244\n",
       "2  0.359188  0.511416  0.988670  0.888591  0.877439  0.110544\n",
       "1  0.692599  0.152252  0.002561  0.759924  0.669620  0.237597\n",
       "0  0.607429  0.785784  0.431967  0.520277  0.313813  0.934551"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_index(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.692599</td>\n",
       "      <td>0.152252</td>\n",
       "      <td>0.002561</td>\n",
       "      <td>0.759924</td>\n",
       "      <td>0.669620</td>\n",
       "      <td>0.237597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.359188</td>\n",
       "      <td>0.511416</td>\n",
       "      <td>0.988670</td>\n",
       "      <td>0.888591</td>\n",
       "      <td>0.877439</td>\n",
       "      <td>0.110544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.671912</td>\n",
       "      <td>0.702555</td>\n",
       "      <td>0.438069</td>\n",
       "      <td>0.030407</td>\n",
       "      <td>0.300120</td>\n",
       "      <td>0.227341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.607429</td>\n",
       "      <td>0.785784</td>\n",
       "      <td>0.431967</td>\n",
       "      <td>0.520277</td>\n",
       "      <td>0.313813</td>\n",
       "      <td>0.934551</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.690845</td>\n",
       "      <td>0.919966</td>\n",
       "      <td>0.811186</td>\n",
       "      <td>0.600167</td>\n",
       "      <td>0.744653</td>\n",
       "      <td>0.052244</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3         4         5\n",
       "1  0.692599  0.152252  0.002561  0.759924  0.669620  0.237597\n",
       "2  0.359188  0.511416  0.988670  0.888591  0.877439  0.110544\n",
       "4  0.671912  0.702555  0.438069  0.030407  0.300120  0.227341\n",
       "0  0.607429  0.785784  0.431967  0.520277  0.313813  0.934551\n",
       "3  0.690845  0.919966  0.811186  0.600167  0.744653  0.052244"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2</th>\n",
       "      <th>1</th>\n",
       "      <th>5</th>\n",
       "      <th>4</th>\n",
       "      <th>0</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.431967</td>\n",
       "      <td>0.785784</td>\n",
       "      <td>0.934551</td>\n",
       "      <td>0.313813</td>\n",
       "      <td>0.607429</td>\n",
       "      <td>0.520277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.002561</td>\n",
       "      <td>0.152252</td>\n",
       "      <td>0.237597</td>\n",
       "      <td>0.669620</td>\n",
       "      <td>0.692599</td>\n",
       "      <td>0.759924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.988670</td>\n",
       "      <td>0.511416</td>\n",
       "      <td>0.110544</td>\n",
       "      <td>0.877439</td>\n",
       "      <td>0.359188</td>\n",
       "      <td>0.888591</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.811186</td>\n",
       "      <td>0.919966</td>\n",
       "      <td>0.052244</td>\n",
       "      <td>0.744653</td>\n",
       "      <td>0.690845</td>\n",
       "      <td>0.600167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.438069</td>\n",
       "      <td>0.702555</td>\n",
       "      <td>0.227341</td>\n",
       "      <td>0.300120</td>\n",
       "      <td>0.671912</td>\n",
       "      <td>0.030407</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          2         1         5         4         0         3\n",
       "0  0.431967  0.785784  0.934551  0.313813  0.607429  0.520277\n",
       "1  0.002561  0.152252  0.237597  0.669620  0.692599  0.759924\n",
       "2  0.988670  0.511416  0.110544  0.877439  0.359188  0.888591\n",
       "3  0.811186  0.919966  0.052244  0.744653  0.690845  0.600167\n",
       "4  0.438069  0.702555  0.227341  0.300120  0.671912  0.030407"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(axis=1, by=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 常用统计计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.604394\n",
       "1    0.614395\n",
       "2    0.534490\n",
       "3    0.559873\n",
       "4    0.581129\n",
       "5    0.312455\n",
       "dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.692599\n",
       "1    0.919966\n",
       "2    0.988670\n",
       "3    0.888591\n",
       "4    0.877439\n",
       "5    0.934551\n",
       "dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.359188\n",
       "1    0.152252\n",
       "2    0.002561\n",
       "3    0.030407\n",
       "4    0.300120\n",
       "5    0.052244\n",
       "dtype: float64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "45"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    3.021972\n",
       "1    3.071973\n",
       "2    2.672452\n",
       "3    2.799365\n",
       "4    2.905644\n",
       "5    1.562277\n",
       "dtype: float64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    5\n",
       "1    5\n",
       "2    5\n",
       "3    5\n",
       "4    5\n",
       "5    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.671912\n",
       "1    0.702555\n",
       "2    0.438069\n",
       "3    0.600167\n",
       "4    0.669620\n",
       "5    0.227341\n",
       "dtype: float64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.604394</td>\n",
       "      <td>0.614395</td>\n",
       "      <td>0.534490</td>\n",
       "      <td>0.559873</td>\n",
       "      <td>0.581129</td>\n",
       "      <td>0.312455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.141375</td>\n",
       "      <td>0.297784</td>\n",
       "      <td>0.382621</td>\n",
       "      <td>0.328482</td>\n",
       "      <td>0.261149</td>\n",
       "      <td>0.356486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.359188</td>\n",
       "      <td>0.152252</td>\n",
       "      <td>0.002561</td>\n",
       "      <td>0.030407</td>\n",
       "      <td>0.300120</td>\n",
       "      <td>0.052244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>0.607429</td>\n",
       "      <td>0.511416</td>\n",
       "      <td>0.431967</td>\n",
       "      <td>0.520277</td>\n",
       "      <td>0.313813</td>\n",
       "      <td>0.110544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>0.671912</td>\n",
       "      <td>0.702555</td>\n",
       "      <td>0.438069</td>\n",
       "      <td>0.600167</td>\n",
       "      <td>0.669620</td>\n",
       "      <td>0.227341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>0.690845</td>\n",
       "      <td>0.785784</td>\n",
       "      <td>0.811186</td>\n",
       "      <td>0.759924</td>\n",
       "      <td>0.744653</td>\n",
       "      <td>0.237597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>0.692599</td>\n",
       "      <td>0.919966</td>\n",
       "      <td>0.988670</td>\n",
       "      <td>0.888591</td>\n",
       "      <td>0.877439</td>\n",
       "      <td>0.934551</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              0         1         2         3         4         5\n",
       "count  5.000000  5.000000  5.000000  5.000000  5.000000  5.000000\n",
       "mean   0.604394  0.614395  0.534490  0.559873  0.581129  0.312455\n",
       "std    0.141375  0.297784  0.382621  0.328482  0.261149  0.356486\n",
       "min    0.359188  0.152252  0.002561  0.030407  0.300120  0.052244\n",
       "25%    0.607429  0.511416  0.431967  0.520277  0.313813  0.110544\n",
       "50%    0.671912  0.702555  0.438069  0.600167  0.669620  0.227341\n",
       "75%    0.690845  0.785784  0.811186  0.759924  0.744653  0.237597\n",
       "max    0.692599  0.919966  0.988670  0.888591  0.877439  0.934551"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = DataFrame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1['product'] = [pd.util.testing.rands(6) for i in range(15)]\n",
    "df1['price'] = [np.random.randint(1000) for i in range(15)] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>product</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AGRShn</td>\n",
       "      <td>861</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>apnCdC</td>\n",
       "      <td>416</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>w5fFVE</td>\n",
       "      <td>157</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>bvljya</td>\n",
       "      <td>593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>egAaov</td>\n",
       "      <td>745</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>WIyg9u</td>\n",
       "      <td>488</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>kxAYs5</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>FLowu5</td>\n",
       "      <td>160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>SEVapZ</td>\n",
       "      <td>109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>y2Drfz</td>\n",
       "      <td>426</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1Btj3V</td>\n",
       "      <td>927</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>HZeQLv</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>apfB0H</td>\n",
       "      <td>343</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Z6bkYc</td>\n",
       "      <td>869</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Ues5U3</td>\n",
       "      <td>877</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   product  price\n",
       "0   AGRShn    861\n",
       "1   apnCdC    416\n",
       "2   w5fFVE    157\n",
       "3   bvljya    593\n",
       "4   egAaov    745\n",
       "5   WIyg9u    488\n",
       "6   kxAYs5     17\n",
       "7   FLowu5    160\n",
       "8   SEVapZ    109\n",
       "9   y2Drfz    426\n",
       "10  1Btj3V    927\n",
       "11  HZeQLv    132\n",
       "12  apfB0H    343\n",
       "13  Z6bkYc    869\n",
       "14  Ues5U3    877"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "bins = [0,200,400,800,1000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = pd.cut(df1['price'], bins)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     (800, 1000]\n",
       "1      (400, 800]\n",
       "2        (0, 200]\n",
       "3      (400, 800]\n",
       "4      (400, 800]\n",
       "5      (400, 800]\n",
       "6        (0, 200]\n",
       "7        (0, 200]\n",
       "8        (0, 200]\n",
       "9      (400, 800]\n",
       "10    (800, 1000]\n",
       "11       (0, 200]\n",
       "12     (200, 400]\n",
       "13    (800, 1000]\n",
       "14    (800, 1000]\n",
       "Name: price, dtype: category\n",
       "Categories (4, interval[int64]): [(0, 200] < (200, 400] < (400, 800] < (800, 1000]]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(400, 800]     5\n",
       "(0, 200]       5\n",
       "(800, 1000]    4\n",
       "(200, 400]     1\n",
       "Name: price, dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count             15\n",
       "unique             4\n",
       "top       (400, 800]\n",
       "freq               5\n",
       "Name: price, dtype: object"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
